package biz.model;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "bbs_post")
public class BbsPost implements Serializable {

	private static final long serialVersionUID = 1L;
	private String id;
	//private String boardid;
	private BbsBoard board;
	//private String userid;
	private BbsUser postuser;
	private String title;
	private String content;
	private String parentid;
	private String toppostid;
	private int level;
	private Timestamp date;
	private String postkey;
	private int sorttype;
	private int essence;
	private List<BbsAttachment> attlist;
	private List<BbsPost> respost;
	private BbsPost parentBean;
	private String historical;
	private Timestamp finaltime;
	 
	@Id
	@GeneratedValue(generator = "pk")
	@GenericGenerator(name = "pk", strategy = "uuid.hex")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

//	public String getBoardid() {
//		return boardid;
//	}
//
//	public void setBoardid(String boardid) {
//		this.boardid = boardid;
//	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "boardid")
	public BbsBoard getBoard() {
		return board;
	}

	public void setBoard(BbsBoard board) {
		this.board = board;
	}

//	public String getUserid() {
//		return userid;
//	}
//
//	public void setUserid(String userid) {
//		this.userid = userid;
//	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "userid")
	public BbsUser getPostuser() {
		return postuser;
	}

	public void setPostuser(BbsUser postuser) {
		this.postuser = postuser;
	}


	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getParentid() {
		return parentid;
	}

	public void setParentid(String parentid) {
		this.parentid = parentid;
	}

	public String getToppostid() {
		return toppostid;
	}

	public void setToppostid(String toppostid) {
		this.toppostid = toppostid;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public Timestamp getDate() {
		return date;
	}

	public void setDate(Timestamp date) {
		this.date = date;
	}

	public String getPostkey() {
		return postkey;
	}

	public void setPostkey(String postkey) {
		this.postkey = postkey;
	}

	public int getSorttype() {
		return sorttype;
	}

	public void setSorttype(int sorttype) {
		this.sorttype = sorttype;
	}

	@OneToMany(mappedBy="attpost", cascade = CascadeType.REFRESH)
	@OrderBy(value = "date desc")
	public List<BbsAttachment> getAttlist() {
		return attlist;
	}

	public void setAttlist(List<BbsAttachment> attlist) {
		this.attlist = attlist;
	}
	@Transient
//	@OrderBy(value = "date desc")
	public List<BbsPost> getRespost() {
		return respost;
	}

	public void setRespost(List<BbsPost> respost) {
		this.respost = respost;
	}

	@Transient
	public BbsPost getParentBean() {
		return parentBean;
	}

	public void setParentBean(BbsPost parentBean) {
		this.parentBean = parentBean;
	}

	public String getHistorical() {
		return historical;
	}

	public void setHistorical(String historical) {
		this.historical = historical;
	}

	public int getEssence() {
		return essence;
	}

	public void setEssence(int essence) {
		this.essence = essence;
	}

	public Timestamp getFinaltime() {
		return finaltime;
	}

	public void setFinaltime(Timestamp finaltime) {
		this.finaltime = finaltime;
	}

	public int hashCode()
	{
		return new HashCodeBuilder().append(getId()).toHashCode();
	}

	public String toString()
	{
		return new ToStringBuilder(this).append("id", getId()).toString();
	}
	
	public boolean equals(Object other)
	{
		if (this == other)
		{
			return true;
		}
		if (!(other instanceof BbsPost))
		{
			return false;
		}
		BbsPost castOther = (BbsPost) other;
		return new EqualsBuilder().append(this.getId(), castOther.getId()).isEquals();
	}

}
